package com.willawn.androidtest.test;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class DateTimeActivity extends Activity {
	private Button btnDate, btnTime;
	private TextView tvwDate, tvwTime;
	private Calendar c;
	private int year, month, day;
	private int hour, minute;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.datetime);

		btnDate = (Button) findViewById(R.id.btnDate);
		btnTime = (Button) findViewById(R.id.btnTime);
		c = Calendar.getInstance();
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH);
		day = c.get(Calendar.DAY_OF_MONTH);
		tvwDate = (TextView) findViewById(R.id.tvwDate);
		tvwDate.setText(year + "-" + month + "-" + day);
		tvwTime = (TextView) findViewById(R.id.tvwTime);
		tvwTime.setText(hour + ":" + minute);

		btnDate.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(0);
			}
		});

		btnTime.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(1);
			}
		});
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		if (id==0) {
			return new DatePickerDialog(this, l1, year, month, day);
		}else{
			return new TimePickerDialog(this, l2, year, month, false);
		}
	}
	
	private OnDateSetListener l1 = new OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			DateTimeActivity.this.year = year;
			DateTimeActivity.this.month = monthOfYear;
			DateTimeActivity.this.day = dayOfMonth;
			tvwDate.setText(year + "-" + month + "-" + day);
		}
	};
	
	private OnTimeSetListener l2 = new OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			DateTimeActivity.this.hour = hourOfDay;
			DateTimeActivity.this.minute = minute;
			tvwTime.setText(hour + ":" + minute);
		}
	};
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
